package com.alks.common.config.handler;

import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;

public class TextHeadEventHandler implements IEventHandler {
    private final String footerText;

    private final PdfFont pdfFont;

    public TextHeadEventHandler(PdfFont pdfFont, String footerText) {
        this.pdfFont = pdfFont;
        this.footerText = footerText;
    }

    public void handleEvent(Event event) {
        PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
        PdfPage page = docEvent.getPage();
        Rectangle pageSize = page.getPageSize();
        PdfCanvas pdfCanvas = new PdfCanvas(page);
        Canvas canvas = new Canvas(pdfCanvas, pageSize);

        float x = pageSize.getLeft() + 40;
        float y = pageSize.getTop() - 25;
        Paragraph title = new Paragraph(footerText).setFontSize(12).setFont(pdfFont);
        canvas.showTextAligned(title, x, y, TextAlignment.LEFT);

        canvas.close();
    }
}